package com.lizhi.updata;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.lizhi.model.Article;

public class ParseNetwork {
	ExecutorService mExecutorService = Executors.newCachedThreadPool();

	public ParseNetwork() {
		String[] urls = {"http://www.lz13.cn/catalog.asp?cate=3&page=","http://www.lz13.cn/catalog.asp?cate=22&page=","http://www.lz13.cn/catalog.asp?cate=14&page=","http://www.lz13.cn/catalog.asp?cate=10&page="};
		String[] typs = {"story","article","office","business"};
		for(int i=3;i<4;i++){
			String type=typs[i];
			for (int j = 1; j <= 10; j++) {
				mExecutorService.execute(new ParseBriefTask(urls[i] + j, String.valueOf(j), type));
			}
		}

	}

	public static void main(String[] args) throws IOException {
		new ParseNetwork();
	}

	class ParseContentTask extends Thread {
		Article mArticle;

		public ParseContentTask(Article article) {
			mArticle = article;
		}

		public void run() {
			try {
				Document document = Jsoup.connect(mArticle.href).get();
				Element element = document.getElementsByClass("PostContent").get(0);
				Elements elements = element.getElementsByTag("p");
				StringBuffer buffer = new StringBuffer();
				for (Element element2 : elements) {
					buffer.append(element2.text()).append("\n");
				}
				System.out.println(buffer.toString());
				mExecutorService.execute(new UploadDataTask("/contents/"+mArticle.type+"/"+ mArticle.id, JSON.toJSONString(buffer.toString())));

			} catch (IOException e) {
				System.err.println(mArticle.href);
				e.printStackTrace();
			}
		}

	}

	class ParseBriefTask extends Thread {
		String mUrl;
		String mIndex;
		String mType;

		public ParseBriefTask(String url, String index, String type) {
			mUrl = url;
			mIndex = index;
			mType = type;
		}

		public void run() {
			try {
				parseBrief(mUrl, mIndex, mType);
			} catch (IOException e) {
				System.err.println(mUrl);
				e.printStackTrace();
			}
		}

		private void parseBrief(String url, String index, String type) throws IOException {
			System.out.println(url);
			Document document = Jsoup.connect(url).get();

			Elements elements = document.getElementsByClass("PostHead");
			Elements elements2 = document.getElementsByClass("PostContent1");
			ArrayList<Article> articles = new ArrayList<Article>(elements.size());
			for (int i = 0; i < elements.size(); i++) {
				Article article = new Article();
				Element element = elements.get(i);
				article.name = element.getElementsByTag("h3").get(0).getElementsByTag("a").text();
				article.href = element.getElementsByTag("h3").get(0).getElementsByTag("a").get(0).attr("href");
				article.id = article.href.substring(article.href.lastIndexOf("/") + 1, article.href.indexOf(".html"));
				article.type=mType;
				Element element2 = elements2.get(i);
				article.brief = element2.text();
				mExecutorService.execute(new ParseContentTask(article));
				articles.add(article);
			}
			System.out.println(JSON.toJSONString(articles));
			mExecutorService.execute(new UploadDataTask("/types/"+mType+"/" + index, JSON.toJSONString(articles)));
		}
	}

}
